package com.nineclock.system.controller;

import com.nineclock.common.entity.Result;
import com.nineclock.system.dto.SysCompanyDTO;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.service.SysCompanyService;
import com.nineclock.system.service.SysCompanyUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("company")
@Api(value = "企业管理接口", tags = "系统服务", description = "企业管理接口")
public class SysCompanyController {
    @Autowired
    private SysCompanyService sysCompanyService;

    @Autowired
    private SysCompanyUserService companyUserService;

    /**
     * 企业管理: 根据用户ID查询关联的企业列表
     * 接口路径：GET/sys/company/list/{userId}
     */
    @ApiOperation(value = "根据用户ID查询关联的企业列表")
    @GetMapping("list/{userId}")
    public Result<List<SysCompanyDTO>> queryUserJoinCompany(@PathVariable Long userId){
        List<SysCompanyDTO> companyDTOList=sysCompanyService.queryUserJoinCompany(userId);
        return Result.success(companyDTOList);
    }

    //企业管理: 查询当前企业信息
    //接口路径：GET/sys/company
    @GetMapping
    @ApiOperation("企业管理：查询当前企业信息")
    public Result<SysCompanyDTO>queryCompanyInfo(){
        SysCompanyDTO companyDTO = sysCompanyService.queryCompanyInfo();
        return Result.success(companyDTO);
    }

    //企业管理: 上传图片
    //接口路径：POST/sys/company/uploadOSS
    @PostMapping("uploadOSS")
    @ApiOperation(value = "企业管理: 上传企业的Logo")
    public Result<String> uploadCompanyLogo(MultipartFile file) throws Exception{
        String logUrl = sysCompanyService.uploadCompanyLogo(file);
        return Result.success(logUrl);
    }

    //企业管理: 更新当前企业的基本信息
    //接口路径：PUT/sys/company
    @ApiOperation(value = "企业管理: 更新当前企业的基本信息")
    @PutMapping
    public Result updateCompanyInfo(@RequestBody SysCompanyDTO sysCompanyDTO){
        sysCompanyService.updateCompanyInfo(sysCompanyDTO);
        return Result.success();
    }

    //企业管理: 获取企业当前主管理员
    //接口路径：GET/sys/company/getCurrentAdmin
    @ApiOperation(value = "获取企业当前主管理员")
    @GetMapping("/getCurrentAdmin")
    public Result<SysCompanyUserDTO> getCurrentAdmin(){
        SysCompanyUserDTO companyUserDTO = companyUserService.queryCurrentAdmin();
        return Result.success(companyUserDTO);
    }

    // 企业管理: 更改系统管理员
    //接口路径：POST/sys/company/config/admin
    @PostMapping("config/admin")
    @ApiOperation("企业管理: 更改系统管理员")
    public Result changeSysAdmin(@RequestBody Map<String, Object> map){
        String code = (String) map.get("code");//获取验证码
        Long userId = Long.valueOf(map.get("userId").toString());//员工id

        sysCompanyService.changeSysAdmin(code, userId);

        return Result.success();

    }
}
